* life-cycle.
*
* An application can be informed when the session is about to end
- * by connecting to the #GtkApplication::quit-requested signal.
+ * by connecting to the #GtkApplication::quit signal.
*
* An application can request the session to be ended by calling
* gtk_application_end_session().
enum {
WINDOW_ADDED,
WINDOW_REMOVED,
- QUIT_REQUESTED,
- QUIT_CANCELLED,
QUIT,
LAST_SIGNAL
};
GList *windows;
gboolean register_session;
- gboolean quit_requested;
#ifdef GDK_WINDOWING_X11
GDBusConnection *session_bus;
GActionMuxer *muxer;
GMenu *combined;
+ GHashTable *inhibitors;
+ gint quit_inhibited;
+ guint next_cookie;
AppleEvent quit_event, quit_reply;
gboolean quitting;
#endif
g_object_unref (application->priv->muxer);
application->priv->muxer = NULL;
+
+ g_hash_table_unref (application->priv->inhibitors);
}
static void
}
}
+static void
+gtk_application_quit (GtkApplication *app)
+{
+ /* we are asked to quit, so don't linger */
+ g_application_set_inactivity_timeout (G_APPLICATION (app), 0);
+}
+
static void
gtk_application_class_init (GtkApplicationClass *class)
{
class->window_added = gtk_application_window_added;
class->window_removed = gtk_application_window_removed;
+ class->quit = gtk_application_quit;
g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
- /**
- * GtkApplication::quit-requested:
- * @application: the #GtkApplication
- *
- * Emitted when the session manager requests that the application
- * exit (generally because the user is logging out). The application
- * should decide whether or not it is willing to quit and then call
- * g_application_quit_response(), passing %TRUE or %FALSE to give its
- * answer to the session manager. It does not need to give an answer
- * before returning from the signal handler; the answer can be given
- * later on, but <emphasis>the application must not attempt to perform
- * any actions or interact with the user</emphasis> in response to
- * this signal. Any actions required for a clean shutdown should take
- * place in response to the #GtkApplication::quit signal.
- *
- * The application should limit its operations until either the
- * #GApplication::quit or #GtkApplication::quit-cancelled signals is
- * emitted.
- *
- * To receive this signal, you need to set the
- * #GtkApplication::register-session property
- * when creating the application object.
- *
- * Since: 3.4
- */
- gtk_application_signals[QUIT_REQUESTED] =
- g_signal_new ("quit-requested", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
- G_STRUCT_OFFSET (GtkApplicationClass, quit_requested),
- NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
-
- /**
- * GtkApplication::quit-cancelled:
- * @application: the #GtkApplication
- *
- * Emitted when the session manager decides to cancel a logout after
- * the application has already agreed to quit. After receiving this
- * signal, the application can go back to what it was doing before
- * receiving the #GtkApplication::quit-requested signal.
- *
- * To receive this signal, you need to set the
- * #GtkApplication::register-session property
- * when creating the application object.
- *
- * Since: 3.4
- */
- gtk_application_signals[QUIT_CANCELLED] =
- g_signal_new ("quit-cancelled", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
- G_STRUCT_OFFSET (GtkApplicationClass, quit_cancelled),
- NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
-
/**
* GtkApplication::quit:
* @application: the #GtkApplication
* should exit as soon as possible after receiving this signal; if
* it does not, the session manager may choose to forcibly kill it.
*
- * Normally, an application would only be sent a ::quit if it
- * agreed to quit in response to a #GtkApplication::quit-requested
- * signal. However, this is not guaranteed; in some situations the
+ * Normally, an application would only be sent a ::quit if there
+ * are no inhibitors (see gtk_application_inhibit()).
+ * However, this is not guaranteed; in some situations the
* session manager may decide to end the session without giving
* applications a chance to object.
*
* Since: 3.4
*/
gtk_application_signals[QUIT] =
- g_signal_new ("quit", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
+ g_signal_new ("quit", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkApplicationClass, quit),
NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
app->priv->client_path = NULL;
}
+static void
+gtk_application_quit_response (GtkApplication *application,
+ gboolean will_quit,
+ const gchar *reason)
+{
+ g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
+
+ g_dbus_proxy_call (application->priv->client_proxy,
+ "EndSessionResponse",
+ g_variant_new ("(bs)", will_quit, reason ? reason : ""),
+ G_DBUS_CALL_FLAGS_NONE,
+ G_MAXINT,
+ NULL, NULL, NULL);
+}
static void
client_proxy_signal (GDBusProxy *proxy,
const gchar *sender_name,
if (strcmp (signal_name, "QueryEndSession") == 0)
{
g_debug ("Received QueryEndSession");
- app->priv->quit_requested = TRUE;
- g_signal_emit (app, gtk_application_signals[QUIT_REQUESTED], 0);
+ gtk_application_quit_response (app, TRUE, NULL);
+ }
+ else if (strcmp (signal_name, "CancelEndSession") == 0)
+ {
+ g_debug ("Received CancelEndSession");
}
else if (strcmp (signal_name, "EndSession") == 0)
{
g_debug ("Received EndSession");
- app->priv->quit_requested = TRUE;
gtk_application_quit_response (app, TRUE, NULL);
unregister_client (app);
g_signal_emit (app, gtk_application_signals[QUIT], 0);
}
- else if (strcmp (signal_name, "CancelEndSession") == 0)
- {
- g_debug ("Received CancelEndSession");
- g_signal_emit (app, gtk_application_signals[QUIT_CANCELLED], 0);
- }
else if (strcmp (signal_name, "Stop") == 0)
{
g_debug ("Received Stop");
g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
}
-/**
- * gtk_application_quit_response:
- * @application: the #GtkApplication
- * @will_quit: whether the application agrees to quit
- * @reason: (allow-none): a short human-readable string that explains
- * why quitting is not possible
- *
- * This function <emphasis>must</emphasis> be called in response to the
- * #GtkApplication::quit-requested signal, to indicate whether or
- * not the application is willing to quit. The application may call
- * it either directly from the signal handler, or at some later point.
- *
- * It should be stressed that <emphasis>applications should not assume
- * that they have the ability to block logout or shutdown</emphasis>,
- * even when %FALSE is passed for @will_quit.
- *
- * After calling this method, the application should wait to receive
- * either #GtkApplication::quit-cancelled or #GtkApplication::quit.
- *
- * If the application does not connect to #GtkApplication::quit-requested,
- * #GtkApplication will call this method on its behalf (passing %TRUE
- * for @will_quit).
- *
- * Since: 3.4
- */
-void
-gtk_application_quit_response (GtkApplication *application,
- gboolean will_quit,
- const gchar *reason)
-{
- g_return_if_fail (GTK_IS_APPLICATION (application));
- g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
- g_return_if_fail (application->priv->client_proxy != NULL);
- g_return_if_fail (application->priv->quit_requested);
-
- application->priv->quit_requested = FALSE;
- g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
-
- g_dbus_proxy_call (application->priv->client_proxy,
- "EndSessionResponse",
- g_variant_new ("(bs)", will_quit, reason ? reason : ""),
- G_DBUS_CALL_FLAGS_NONE,
- G_MAXINT,
- NULL, NULL, NULL);
-}
/**
* GtkApplicationInhibitFlags:
* Requests that the session manager end the current session.
* @style indicates how the session should be ended, and
* @request_confirmation indicates whether or not the user should be
- * given a chance to confirm the action. Both of these flags are merely
- * hints though; the session manager may choose to ignore them.
+ * given a chance to confirm the action. Both of these parameters are
+ * merely hints though; the session manager may choose to ignore them.
*
* Return value: %TRUE if the request was sent; %FALSE if it could not
* be sent (eg, because it could not connect to the session manager)
/* OS X implementation copied from EggSMClient */
-static gboolean
-idle_quit_requested (gpointer client)
-{
- g_signal_emit (client, gtk_application_signals[QUIT_REQUESTED], 0);
-
- return FALSE;
-}
-
-static pascal OSErr
-quit_requested (const AppleEvent *aevt,
- AppleEvent *reply,
- long refcon)
-{
- GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
-
- g_return_val_if_fail (!app->priv->quit_requested, userCanceledErr);
-
- /* FIXME AEInteractWithUser? */
- osx->quit_requested = TRUE;
- AEDuplicateDesc (aevt, &app->priv->quit_event);
- AEDuplicateDesc (reply, &app->priv->quit_reply);
- AESuspendTheCurrentEvent (aevt);
-
- /* Don't emit the "quit_requested" signal immediately, since we're
- * called from a weird point in the guts of gdkeventloop-quartz.c
- */
- g_idle_add (idle_quit_requested, app);
-
- return noErr;
-}
-
-static void
-gtk_application_startup_session_quartz (GtkApplication *app)
-{
- if (app->priv->register_session)
- AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerUPP (quit_requested),
- (long)GPOINTER_TO_SIZE (app), false);
-}
-
static pascal OSErr
quit_requested_resumed (const AppleEvent *aevt,
AppleEvent *reply,
{
GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
- app->priv->quit_requested = FALSE;
-
- return app->priv->quitting ? noErr : userCanceledErr;
+ return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
}
static gboolean
AEDisposeDesc (&app->quit->quit_event);
AEDisposeDesc (&app->quit->quit_reply);
- if (app->priv->quitting)
+ if (app->priv->quit_inhibit == 0)
g_signal_emit (app, gtk_application_signals[QUIT], 0);
return FALSE;
}
-void
-gtk_application_quit_response (GtkApplication *application,
- gboolean will_quit,
- const gchar *reason)
+static pascal OSErr
+quit_requested (const AppleEvent *aevt,
+ AppleEvent *reply,
+ long refcon)
{
- g_return_if_fail (GTK_IS_APPLICATION (application));
- g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
- g_return_if_fail (application->priv->quit_requested);
+ GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
- application->priv->quitting = will_quit;
+ /* FIXME AEInteractWithUser? */
+ AEDuplicateDesc (aevt, &app->priv->quit_event);
+ AEDuplicateDesc (reply, &app->priv->quit_reply);
+ AESuspendTheCurrentEvent (aevt);
- /* Finish in an idle handler since the caller might have called
- * gtk_application_quit_response() from inside the ::quit-requested
- * signal handler, but may not expect the ::quit signal to arrive
- * during the gtk_application_quit_response() call.
+ /* Don't emit the "quit" signal immediately, since we're
+ * called from a weird point in the guts of gdkeventloop-quartz.c
*/
- g_idle_add (idle_will_quit, application);
+ g_idle_add (idle_will_quit, app);
+
+ return noErr;
+}
+
+static void
+gtk_application_startup_session_quartz (GtkApplication *app)
+{
+ if (app->priv->register_session)
+ AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
+ NewAEEventHandlerUPP (quit_requested),
+ (long)GPOINTER_TO_SIZE (app), false);
+
+ app->priv->inhibitors = g_hash_table_new (NULL, NULL);
}
guint
GtkApplicationInhibitFlags flags,
const gchar *reason)
{
- return 0;
+ guint cookie;
+
+ g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
+ g_return_val_if_fail (flags != 0, 0);
+
+ application->priv->next_cookie++;
+ cookie = application->priv->next_cookie;
+
+ g_hash_table_insert (application->priv->inhibitors,
+ GUINT_TO_POINTER (cookie),
+ GUINT_TO_POINTER (flags));
+
+ if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
+ application->priv->quit_inhibit++;
+
+ return cookie;
}
void
gtk_application_uninhibit (GtkApplication *application,
guint cookie)
{
+ GApplicationInhibitFlags flags;
+
+ flags = GPOINTER_TO_UINT (g_hash_table_lookup (application->priv->inhibitors, GUINT_TO_POINTER (cookie)));
+
+ if (flags == 0)
+ {
+ g_warning ("Invalid inhibitor cookie");
+ return;
+ }
+
+ g_hash_table_remove (application->priv->inhibitors, GUINT_TO_POINTER (cookie));
+
+ if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
+ application->priv->quit_inhibit--;
}
gboolean
gtk_application_is_inhibited (GtkApplication *application,
GtkApplicationInhibitFlags flags)
{
+ if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
+ return application->priv->quit_inhibit > 0;
+
return FALSE;
}
* http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
*/
-void
-gtk_application_quit_response (GtkApplication *application,
- gboolean will_quit,
- const gchar *reason)
-{
-}
-
guint
gtk_application_inhibit (GtkApplication *application,
GtkWindow *window,
#include <gtk/gtk.h>
-static gboolean will_quit = TRUE;
-static gchar *reason;
static GtkWidget *inhibit_entry;
static GtkWidget *inhibit_logout;
static GtkWidget *inhibit_switch;
gtk_application_end_session (app, style, confirm);
}
-static void
-toggle_will_quit (GtkToggleButton *button, gpointer data)
-{
- will_quit = gtk_toggle_button_get_active (button);
-}
-
-static void
-reason_changed (GtkEntry *entry, GParamSpec *pspec, GtkApplication *app)
-{
- g_free (reason);
- reason = g_strdup (gtk_entry_get_text (entry));
-}
-
static void
inhibitor_toggled (GtkToggleButton *button, GtkApplication *app)
{
GtkWidget *separator;
GtkWidget *grid;
GtkWidget *button;
- GtkWidget *entry;
GtkWidget *label;
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_add (GTK_CONTAINER (box), grid);
- button = gtk_check_button_new_with_label ("Will quit");
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), will_quit);
- g_signal_connect (button, "toggled",
- G_CALLBACK (toggle_will_quit), NULL);
- gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
-
- entry = gtk_entry_new ();
- g_signal_connect (entry, "notify::text",
- G_CALLBACK (reason_changed), app);
- gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);
-
- separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
- gtk_container_add (GTK_CONTAINER (box), separator);
-
- grid = gtk_grid_new ();
- gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
- gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
-
- gtk_container_add (GTK_CONTAINER (box), grid);
-
end_combo = gtk_combo_box_text_new ();
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "logout", "Logout");
gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "reboot", "Reboot");
gtk_application_add_window (app, GTK_WINDOW (win));
}
-static void
-quit_requested (GtkApplication *app,
- gpointer data)
-{
- g_print ("Received quit-requested, reply: %d, '%s'\n", will_quit, reason);
- gtk_application_quit_response (app, will_quit, reason);
-}
-
static void
quit (GtkApplication *app,
gpointer data)
g_print ("Received quit\n");
}
-static void
-quit_cancelled (GtkApplication *app,
- gpointer data)
-{
- g_print ("received quit-cancelled\n");
-}
-
int
main (int argc, char *argv[])
{
g_signal_connect (app, "activate",
G_CALLBACK (activate), NULL);
- g_signal_connect (app, "quit-requested",
- G_CALLBACK (quit_requested), NULL);
g_signal_connect (app, "quit",
G_CALLBACK (quit), NULL);
- g_signal_connect (app, "quit-cancelled",
- G_CALLBACK (quit_cancelled), NULL);
g_application_run (G_APPLICATION (app), argc, argv);